package mywebapp.dao.visiteur.struts;
import mywebapp.dao.Champ;
import mywebapp.dao.ETypeSimple;
import mywebapp.dao.Entity;
import mywebapp.dao.TypeSimple;
import mywebapp.dao.impl.TypeEntityImpl;
import mywebapp.dao.impl.TypeSimpleImpl;
import mywebapp.dao.visiteur.VisiteurEntity;

public class VisiteurModelJavaStruts implements VisiteurEntity{

	@Override
	public String visiter(Entity e) {
		String classContenue = "";
		classContenue += "public class " + makeFirstMaj(e.getName().toLowerCase()) + " {\n\n";

		classContenue += "\tprivate Long ID;\n\n";
		classContenue += "\tpublic Long getID(){ return this.ID;}\n\n";
		classContenue += "\tpublic void setID(Long value){ this.ID = value;}\n\n";

		for(Object  o1 : e.getChamps()){
			String nameType = "";
			Champ c = (Champ) o1;

			if(c.getType() instanceof TypeSimple){
				TypeSimpleImpl t = (TypeSimpleImpl) c.getType();
				switch(t.getType().getValue()){
				case ETypeSimple.BIGINT :
					nameType = "long" ;
					break;
				case ETypeSimple.STRING :
					nameType =  "String";
					break;
				case ETypeSimple.DATE:
					classContenue = "import java.util.Date;\n" + classContenue;
					nameType = "Date";
					break;
				case ETypeSimple.FLOAT:
					nameType = "float";
					break;
				case ETypeSimple.INT:
					nameType = "int";
					break;
				}
			}else{
				TypeEntityImpl t = (TypeEntityImpl) c.getType();
				nameType = t.getType().getName().toLowerCase();
			}


			if(c.isIsList()){
				classContenue = "import java.util.ArrayList;\n" + classContenue;
				if(c.getType() instanceof TypeEntityImpl)
					nameType = "ArrayList<"+nameType+">";
				else
					nameType = "ArrayList";
			}

			classContenue += "\tprivate "+ nameType + " " + c.getName().toLowerCase() + ";\n\n";
			classContenue += "\tpublic "+ nameType+ " get"+c.getName().toLowerCase()+"(){ return this."+c.getName().toLowerCase()+" ;}\n\n";
			classContenue += "\tpublic void set"+c.getName().toLowerCase()+"("+nameType + " value){ this."+c.getName().toLowerCase()+" = value;}\n\n";
		}
		classContenue += "}";
		return classContenue;
	}
	public  String makeFirstMaj(String name){
		return name.replaceFirst(".",(name.charAt(0)+"").toUpperCase());
	}

}



